Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUseGetUserProfile: false,
    // 用户历史记录
    userRecord: [],
    _openid: ''
  },

  onLoad: function (options) {
    // 定义一个变量，如果在本地缓存找不到用户数据，则去数据库查询
    let flag = false
    // 获取云函数，拿到openid
    wx.cloud.callFunction({
      name: "userOpenId",
      success: res => {
        // 将从云函数拿到的openid赋值给data里面的变量
        this.data._openid = res.result.openid
        // 获取缓存数据
        let test = [wx.getStorageSync('userRecord')]
        // console.log(test)
        // 将缓存数据重新赋值给用户记录
        this.data.userRecord = test
        // console.log(this.data.userRecord[0])
        for (let i = 0; i < this.data.userRecord[0].length; i++) {
          if (this.data._openid == this.data.userRecord[0][i].openid) {
            console.log('在数据缓存找到用户对于的openid，将用户信息赋值给渲染列表，登录成功')
            this.setData({
              userInfo: this.data.userRecord[0][i],
              hasUserInfo: true
            })
            flag = false
            return
          } else {
            flag = true
          }
        }
        // 如果在数据缓存中找不到用户信息则调用云数据库 用户表循环判断是否有过此用户的记录，根据openid来对比判断
        if (flag === true) {
          // 云数据库初始化
          const db = wx.cloud.database()
          const userInfo = db.collection('userInfo')
          userInfo.where({
            openid: this.data._openid
          }).get().then(res => {
            this.setData({
              userInfo: res.data[0],
              hasUserInfo: true
            })
            console.log('在数据寻找到用户数据，自动登录')
            return
          })
        }
      }
    })
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
  },
  // 获取用户信息
  getUserProfile(e) {
    console.log(this.data.userRecord)
    // 获取用户信息
    wx.getUserProfile({
      desc: '用于完善会员资料',
      success: (res) => {
        console.log(res.userInfo)
        console.log(this.data.userRecord)
        // 将用户的信息push到一个临时数组
        this.data.userRecord.push({
          openid: this.data._openid,
          nickName: res.userInfo.nickName,
          gender: res.userInfo.gender,
          avatarUrl: res.userInfo.avatarUrl
        })
        // 将获取到的数据存到缓存当中
        wx.setStorageSync('userRecord', this.data.userRecord)
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true,
          userRecord: this.data.userRecord
        })
        // 同时也存一份到数据库
        const db = wx.cloud.database()
        const userInfo = db.collection('userInfo')
        userInfo.add({
          data: {
            openid: this.data._openid,
            nickName: res.userInfo.nickName,
            gender: res.userInfo.gender,
            avatarUrl: res.userInfo.avatarUrl
          }
        }).then(res => {
          console.log(res)
        })
      }
    })
  },
  // 点击跳转到常见问题页面
  toProblem: function () {
    wx.navigateTo({
      url: '../../pages/my/problem/problem',
    })
  },
  // 点击跳转到账号与安全页面
  toSafe: function () {
    wx.navigateTo({
      url: '../../pages/my/safe/safe',
    })
  },
  // 点击拨打电话
  onCallPhone: function () {
    wx.makePhoneCall({
      phoneNumber: '10086',
    })
  }
})